#include "StdAfx.h"
#include "IOCPConnectionPool.h"
#include <sstream>

CIOCPConnectionPool::CIOCPConnectionPool():
m_pMainIOCP(NULL)
{
}

CIOCPConnectionPool::~CIOCPConnectionPool(void)
{
	Close();
}
bool CIOCPConnectionPool::Create(CIOCPModel* _pIOCP)
{
	m_pMainIOCP = _pIOCP;
	return true;
}
void CIOCPConnectionPool::Close()
{
	m_pMainIOCP = NULL;
}

bool CIOCPConnectionPool::InsertConnection(const std::string &_strIP, int _nPort, CIOCPAsynConnectionSocket* _pConnection)
{
	std::ostringstream out;
	out<<_strIP;
	out<<":";
	out<<_nPort;
	std::string strConnectionName = out.str();

	std::map<std::string,CIOCPAsynConnectionSocket*>::iterator  iter = m_mapConnection.find(strConnectionName);

	if (iter != m_mapConnection.end())
	{
		return false;
	}

	m_mapConnection[strConnectionName] = _pConnection;
	
	return true;

}
CIOCPAsynConnectionSocket* CIOCPConnectionPool::GetConnection(const std::string &_strIP, int _nPort)
{
	CIOCPAsynConnectionSocket *pConnection = NULL;

	std::ostringstream out;
	out<<_strIP;
	out<<":";
	out<<_nPort;
	std::string strConnectionName = out.str();

	std::map<std::string,CIOCPAsynConnectionSocket*>::iterator  iter = m_mapConnection.find(strConnectionName);

	if (iter != m_mapConnection.end())
	{
		pConnection = iter->second;
	}

	return pConnection;
}